package com.dinadi.yidi.http;

import android.app.Activity;
import android.graphics.Bitmap;


import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by dell on 2016/3/21.
 */
public class RestClient {


    /**
     * get请求
     */
    public static void get(String url,  Map<String,String> params, MyHttpCallback callback) {

        OkHttpUtils.get()
                .url(url)
                .params(params)
                .build()
                .connTimeOut(5000)
                .execute(callback);
    }
    public static void doGet(String url,Map<String,String> headParams,  Map<String,String> params, MyHttpCallback callback) {

        OkHttpUtils.get()
                .url(url)
                .headers(headParams)
                .params(params)
                .build()
                .connTimeOut(5000)
                .execute(callback);
    }


    /**
     * post请求
     */
    public static void post(String url,  Map<String,String> params, MyHttpCallback callback) {

        OkHttpUtils.post()
                .url(url)
                .params(params)
                .build()
                .connTimeOut(5000)
                .execute(callback);
    }

    public static void post(Activity tag, String url, JSONObject params, MyHttpCallback callback) {
        OkHttpUtils.post()
                .url(url)
                .params(getParams(params))
                .tag(tag)
                .build()
                .connTimeOut(5000)
                .readTimeOut(5000)
                .execute(callback);
    }


    /**
     * 下载apk
     */
    public static void downAPK(String url, FileHttpCallback callBack) {
        OkHttpUtils//
                .get()//
                .url(url)//
                .build()//
                .connTimeOut(5000)
                .readTimeOut(5000)
                .execute(callBack);
//                .execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "zamai.apk")//
//                {
//                    @Override
//                    public void inProgress(float progress, long total) {
////                        mProgressBar.setProgress((int) (100 * progress));
//                    }
//
//                    @Override
//                    public void onError(Call call, Exception e) {
//
//                    }
//
//                    @Override
//                    public void onResponse(File file) {
//
//
//                    }
//                });
    }

    public static void postBitmap(String url, BitmapHttpCallBack callback) {

        OkHttpUtils.post()
                .url(url)
                .build()
                .connTimeOut(5000)
                .readTimeOut(5000)
                .execute(new BitmapCallback() {
                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(Bitmap response) {

                    }
                });
    }


    /**
     * 上传文件
     * @param url
     * @param params
     * @param file
     * @param callback
     */
    public static void uploadFile(String url, JSONObject params, String fileTag, String fileName, File file, MyHttpCallback callback){
        /*Map<String, String> map = getParams(params);
        String result = String.valueOf(new JSONObject(map));*/


        OkHttpUtils.post()//
                .url(url)
                .params(getParams(params))
                .addFile(fileTag, fileName, file)
                .build()//
                .connTimeOut(5000)
                .readTimeOut(5000)
                .execute(callback);

    }


    public static void cancelRequest(Activity tag) {
        OkHttpUtils.getInstance().cancelTag(tag);
    }

    /**
     * post参数
     *
     * @param jsonObject
     * @return
     */
    public static Map<String, String> getParams(JSONObject jsonObject) {

//        setCommonParams(jsonObject);
//        LogUtils.i("post参数：" + jsonObject.toString());
        Map<String, String> params = new HashMap<>();
        /*try {
            jsonObject.put("loginuser", CommonUtil.get4SP(GlobalAttribute.IPHONE, ""));
        } catch (JSONException e) {
            e.printStackTrace();
        }*/

        params.put("paramJson", jsonObject.toString());
        //        params.put("Signature", DESCryptUtil.encrypt(jsonObject.toString(), GlobalConstants.DES_KEY));
        //        params.put("Token", DESCryptUtil.encrypt(token, GlobalConstants.TOKEN));

        return params;
    }
}
